﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SGIMail.Models;
using SGIMail.DAL;
using System.Data;
using System.Data.Entity;
using System.Net;
using System.Net.Mail;
using SGIMail.BL;
using System.IO;
using System.Threading.Tasks;

namespace SGIMail.WebClient.Controllers
{
    [Authorize]
    public class MailOutController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();
        DbAccess dbAccess = new DbAccess();

        //
        // GET: /MailOut/
        public ActionResult Index()
        {
            var eventList = from events in db.Events
                            where events.Date > DateTime.Today
                            select events;
            //ViewBag.List = new SelectList(eventList, "EventId", "Name");
            MailOutViewModel movm = new MailOutViewModel();

            var contactList = db.Contacts.Include(c => c.Company).OrderByDescending(x => x.Company.CompanyId);
            List<ContactViewModel> mailRecipients = new List<ContactViewModel>();

            foreach (var c in contactList)
            {
                ContactViewModel cvm = new ContactViewModel();
                cvm.ID = c.ContactId;
                cvm.Name = c.Name;
                cvm.Email = c.Email;
                
                if (c.Company != null)
                    cvm.Company = c.Company.CompanyName;

                mailRecipients.Add(cvm);

            }

            movm.recipients = mailRecipients;

            //kan vara så att det behövs vara new selectlistitem istället för selectlist
            movm.listan = new SelectList(eventList, "EventId", "Name");

            return View(movm);
        }

        //
        // GET: /MailOut/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /MailOut/Create
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /MailOut/CreateMail
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> CreateMail(MailOutViewModel movm)
        {
            if (ModelState.IsValid)
            {
                MailHelper mh = new MailHelper();
                await mh.SendMail(movm);
                return RedirectToAction("Index");
            }


            var eventList = from events in db.Events
                            where events.Date > DateTime.Today
                            select events;
            movm.listan = new SelectList(eventList, "EventId", "Name");

            //return RedirectToAction("Index");
            return View("Index", movm);
        }

        //
        // GET: /MailOut/Edit/5
        public async Task<ActionResult> redirectToRegistration(int eventId, int contactId)
        {
            //EventDetails evDe = new EventDetails { Category = Category.clicked, Date = DateTime.Now, EventId = eid, ContactId = cid };
            //db.EventDetails.Add(evDe);
            //db.SaveChanges();

            //var currentEvent = (from events in db.Events
            //                    where events.EventId == eid
            //                    select events).First();

            //string registrationUrl = currentEvent.RegistrationURL;

            string registrationUrl = "";

            using (dbAccess = new DbAccess())
            {
                await dbAccess.RegisterEvent(contactId, eventId, Category.Clicked);
                registrationUrl = dbAccess.GetEventRegistrationUrl(eventId);
            }

            return Redirect(registrationUrl);
        }

        //
        // POST: /MailOut/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /MailOut/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /MailOut/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
